Component({
  options: {
    addGlobalClass: true
  },
  properties: {
    RoomData: {
      type: Object,
      value: {}
    }
  },
  data: {
    PwordVisible: false,
    RightPW: '',
    InputtedPW: '',
    ThisroomId: '',
    ThisroomText: '',

    // 房间内的歌曲列表（只有songMid)
    PlayList: [],

    // 接收请求的房间内用户数据（ID）
    RoomUsers: []
  },
  methods: {
    enterRoom: function() {
      const roomId = this.data.RoomData.roomId;
      const roomText = this.data.RoomData.roomDiscription;
      const roomPassword = this.data.RoomData.roomPassword;
      const uid = wx.getStorageSync('uid');
      const roomid= wx.getStorageSync('RoomId');
      wx.setStorageSync('RoomId', roomId);
      if(roomPassword != '') {
        this.setData({
          PwordVisible: true,
        })
      } else {
        wx.navigateTo({
          url: `/pages/room/room?roomText=${this.data.ThisroomText}`
        });
      }
      this.setData({
        RightPW: roomPassword,
        ThisroomId: roomId,
        ThisroomText: roomText,
      });wx.request({
        
        url: 'http://localhost:3000/RoomAddUser',
        method: 'POST',
        header: {
            'content-type': 'application/json',
            'Authorization': wx.getStorageSync('usertoken')
        },
        data: {
            uid: uid,
            rid: roomid
        },
        success(res) {
            console.log('正在添加用户到房间');
            if (res.statusCode === 202) {
                console.log('用户成功进入房间', res.data);
                
            } else if (res.statusCode === 252) {
                console.error('用户进入房间失败', res.data);
               
            } else if (res.data.code === 401) {
                utils.userlogin();
            } 
            // else {
            //     console.error('请求失败，状态码:', res.statusCode);
            // }
        },
        fail(err) {
            console.error('请求失败', err);
        }
    });

    },
    InputPW:function(e) {
      const InputtedPW =this.data.InputtedPW;
      
      this.setData({
        InputtedPW: e.detail.value
      })
    },
  
    ConfirmPW:function() {
      if(this.data.InputtedPW === this.data.RightPW) {
        console.log("密码正确");
        wx.showToast({
          title: '密码正确',
          icon: 'success',
          duration: 1000
        })
        this.setData({
          PwordVisible: false,
        });
        wx.navigateTo({
          url: `/pages/room/room?roomText=${this.data.ThisroomText}&PlayList=${this.data.PlayList}`
        });
      }else if(this.data.InputtedPW === '') {
        wx.showToast({
          title: '请输入密码',
          icon: 'none',
          duration: 1000
        })
        console.log("请输入密码");
      }
      else {
        wx.showToast({
          title: '密码错误',
          icon: 'none',
          duration: 1000
        })
        console.log("密码错误");
      }
    },

    CloseModal:function() {
      this.setData({
        PwordVisible: false,
      });
    }
  },
})